package com.ccrfid.rmc6.service.personnel;

import com.ccrfid.rmc6.entity.personnel.PersonnelEntity;
import com.ccrfid.rmc6.pojo.amq.AmqPersonnel;
import com.ccrfid.rmc6.pojo.amq.AmqPersonnelCardBinding;
import com.ccrfid.rmc6.service.amq.MqMsgProducer;
import com.ccrfid.rmc6.util.JsonUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
public class PersonnelServiceAspect {
    private static final Logger logger = LoggerFactory.getLogger(PersonnelServiceAspect.class);
    @Autowired
    private MqMsgProducer mqMsgProducer;

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.PersonnelService.createPersonnel(..))",
            returning = "ret")
    public void afterCreatePersonnel(PersonnelEntity ret) {
        AmqPersonnel msgPersonnel = new AmqPersonnel(ret);
        String msg = JsonUtil.INSTANCE.writeValueAsString(msgPersonnel);
        mqMsgProducer.send2Topic("ws_personnel_add", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.PersonnelService.createPrisoner(..))",
            returning = "ret")
    public void afterCreatePrisoner(PersonnelEntity ret) {
        AmqPersonnel msgPersonnel = new AmqPersonnel(ret);
        String msg = JsonUtil.INSTANCE.writeValueAsString(msgPersonnel);
        mqMsgProducer.send2Topic("ws_personnel_add", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.PersonnelService.updatePersonnel(..))",
            returning = "ret")
    public void afterUpdatePersonnel(PersonnelEntity ret) {
        AmqPersonnel msgPersonnel = new AmqPersonnel(ret);
        String msg = JsonUtil.INSTANCE.writeValueAsString(msgPersonnel);
        mqMsgProducer.send2Topic("ws_personnel_update", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.personnel.PersonnelService.deletePersonnels(..)) && args(idList)",
            argNames = "idList")
    public void afterDeletePersonnels(List<Long> idList) {
        String msg = JsonUtil.INSTANCE.writeValueAsString(idList);
        mqMsgProducer.send2Topic("ws_personnel_delete", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.lbservice3.mq.MsgDispatcher.bindPersonnelCard(..)) && args(personnelId, cardNumber)",
            argNames = "personnelId, cardNumber")
    public void afterBindPersonnelCard(Long personnelId, Long cardNumber) {
        AmqPersonnelCardBinding msgBinding = new AmqPersonnelCardBinding(personnelId, cardNumber);
        String msg = JsonUtil.INSTANCE.writeValueAsString(msgBinding);
        mqMsgProducer.send2Topic("ws_personnel_card_bind", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.lbservice3.mq.MsgDispatcher.unBindPersonnelCard(..)) && args(personnelId, cardNumber)",
            argNames = "personnelId, cardNumber")
    public void afterUnBindPersonnelCard(Long personnelId, Long cardNumber) {
        AmqPersonnelCardBinding msgBinding = new AmqPersonnelCardBinding(personnelId, cardNumber);
        String msg = JsonUtil.INSTANCE.writeValueAsString(msgBinding);
        mqMsgProducer.send2Topic("ws_personnel_card_unbind", msg);
    }
}
